var express = require('express');
var router = express.Router();
var DB = require('../lib/mysql');
var resBody = require('../lib/common/response');

router.get('/list', async (req, res, next) => {
	var resArr = await DB('select * from question_bank where del_flg = 0');
	return res.send(resBody(true,resArr));
});

router.post('/add', async (req, res, next) => {
	var question = req.body;
	if(!!!question.title){
		return res.send(resBody(false,{},'标题不能为空'));
	}
	if(!!!question.content){
		return res.send(resBody(false,{},'内容不能为空'));
	}
	if(!!question.id){
		var resObj = await DB(`update question_bank set title ='${question.title}',content = '${question.content}' where id = ${question.id}`);
	}else{
		var resObj = await DB(`insert into question_bank (title,content) values ('${question.title}','${question.content}')`);
		question.id = resObj.insertId;
	}
	return res.send(resBody(!!question.id,question));
});

router.get('/del', async (req, res, next) => {
	var questionId = req.query.id;
	var resObj = await DB(`update question_bank set del_flg = 1 where id = ${questionId}`);
	return res.send(resBody(resObj.changedRows));
});

module.exports = router;